import axios from 'axios';
import Cookies from 'js-cookie';


//axios.defaults.withCredentials = true; //意思是携带cookie信息,保持session的一致性，不能加这个，否则不能跨域
let frontservice = axios.create({
        timeout: 60000,
		//withCredentials: true, //不能加这个，否则不能跨域
		crossDomain: true
});


frontservice.interceptors.request.use(
    config => {
        config.headers['fronttoken'] = Cookies.get('fronttoken') || '';
        config.headers['fronttoken'] = localStorage.getItem('fronttoken');
        config.headers['domainname'] = Cookies.get('frontdomainname')||'';
        config.headers['domainname'] = localStorage.getItem('domainname');
        return config;
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

frontservice.interceptors.response.use(
    response => {
        if (response.status === 200) {
            if ((typeof response.data == 'string') && response.data.constructor == String) {
            try{
              let data=eval('('+response.data+')'); // 如果抛出异常，则会从这条语句终止，被catch捕捉
                return data;
            }catch(e){
                return response.data;
             }
           }else{
            return response.data;
           }
        } else {
            Promise.reject();
        }
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

export default frontservice;